/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @packageName com.ktjiaoyu.service.impl
 * @fileName SysUserServiceImpl.java
 * @createTime 2024/10/1 22:06
 * @lastModify 2024/10/1 22:06
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserServiceImpl.class);

    // @Resource
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> findList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    @Override
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++) {
            // 拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            if(i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响行数
            rows += sysUserMapper.insert(sysUser);
        }
        // 返回总受影响行数
        return rows;
    }

    @Override
    public SysUser login(String account, String password) {
        SysUser loginUser= sysUserMapper.getByAccount(account);
        if (loginUser != null && loginUser.getPassword().equals(password)){
            return loginUser;
        }
        return null;
    }

    @Override
    public Page<SysUser> findPage(SysUser entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<SysUser> page=null;
        try{
            page=new Page<SysUser>();
            page.setPageSize(pageSize);
            Long count= sysUserMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);

            List<SysUser> data=sysUserMapper.getPage(entity,page.offset(),page.getPageSize());
            page.setData(data);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.findPage(SysUser.Integer,Integer)方法出现异常。",e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(SysUser entity) throws Exception {
        try {
            if (entity.getId()==null){
                return sysUserMapper.insert(entity)>0;
            }else {
                return sysUserMapper.update(entity)>0;
            }
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.saveOrUpdate(SysUser)方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public SysUser findById(Long id) throws Exception {
        try {
            return sysUserMapper.getById(id);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.findById(Long)方法出现异常",e);
            throw e;
        }
    }

    // 提供setter方法用于注入属性
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public SysUser findByAccount(String account) throws Exception {
        try {
            return sysUserMapper.getByAccount(account);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.findAccount(String)方法出现异常",e);
            throw e;
        }
    }

    @Override
    public int deleteById(Long id) throws Exception {
        try {
            return sysUserMapper.deleteById(id);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.deleteById(Long)方法出现异常",e);
            throw e;
        }
    }

    @Override
    public List<SysUser> selByRoleId(Long roleId) throws Exception {
        try {
            return sysUserMapper.selByRoleId(roleId);
        }catch (Exception e){
            LOGGER.error("执行SysUserServiceImpl.selByRoleId(Long)方法出现异常",e);
            throw e;
        }
    }
}
